package com.springmvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;

import com.springmvc.entity.Items;

/**
 * @author Administrator
 *
 */
public class ItemsController2 implements HttpRequestHandler {

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Items> items = new ArrayList<>();
		
		Items items_1 = new Items();
		items_1.setName("Mac Pro");
		items_1.setPrice(15000f);
		items_1.setDetail("质量很好");
		
		Items items_2 = new Items();
		items_2.setName("iPad4");
		items_2.setPrice(2800f);
		items_2.setDetail("质量很好，值得信赖");
		
		items.add(items_1);
		items.add(items_2);

		//设置模型数据
		request.setAttribute("itemsList", items);
		
		//通过转发到jsp页面
		request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
		
		//使用此方式可以修改response，设置响应的数据格式，比如json
		/*response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().println("json数据");*/
	}

}
